package com.thiscc.sum.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.thiscc.sum.common.MongoDBManager;
import com.thiscc.sum.utils.DBObjectUtils;
import com.thiscc.sum.utils.ResponseUtils;
import com.thiscc.sum.utils.TableNameConstant;

/**
 * 刷数据专用类
 * @author Einstein
 *
 */
@Controller
@RequestMapping("processData")
public class ProcessDataControll extends CommonController {
    private MongoDBManager mongo = MongoDBManager.getInstance();

    /**
     * 刷阅我人数
     * @param request
     * @param response
     */
    @RequestMapping("processRead")
    public void processRead(HttpServletRequest request, HttpServletResponse response) {

        List<DBObject> list = mongo.findAll(TableNameConstant.T_SUMMARIZE_CONTENT);
        int count = 0;
        for (DBObject content : list) {
            List<DBObject> consigneeList = DBObjectUtils.getList(content, "consignee");
            int readCount = 0;
            for (DBObject consignee : consigneeList) {
                if ("1".equals(DBObjectUtils.getString(consignee, "state"))) {
                    readCount++;
                }
            }
            if (readCount != DBObjectUtils.getInt(content, "aread")) {
                count++;
                //开始刷人数了咯
                DBObject setBean = new BasicDBObject();
                setBean.put("aread", readCount);
                mongo.update(TableNameConstant.T_SUMMARIZE_CONTENT, new BasicDBObject("_id", content.get("_id")), setBean);
            }
        }
        ResponseUtils.send(response, "总共刷了" + count + "条数据");
    }
}
